package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.business.domain.FileBorrowingRegistration;
import com.ruoyi.business.domain.FilePerson;
import com.ruoyi.business.mapper.FileBorrowingRegistrationMapper;
import com.ruoyi.business.service.IFileBorrowingRegistrationService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateExtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 借阅登记Service业务层处理
 *
 * @author sal
 * @date 2024-11-12
 */
@Service
public class FileBorrowingRegistrationServiceImpl implements IFileBorrowingRegistrationService {
    @Autowired
    private FileBorrowingRegistrationMapper fileBorrowingRegistrationMapper;

    /**
     * 查询借阅登记
     *
     * @param id 借阅登记ID
     * @return 借阅登记
     */
    @Override
    public FileBorrowingRegistration selectFileBorrowingRegistrationById(Long id) {
        return fileBorrowingRegistrationMapper.selectFileBorrowingRegistrationById(id);
    }

    /**
     * 查询借阅登记列表
     *
     * @param fileBorrowingRegistration 借阅登记
     * @return 借阅登记
     */
    @Override
    public List<FileBorrowingRegistration> selectFileBorrowingRegistrationList(FileBorrowingRegistration fileBorrowingRegistration) {
        return fileBorrowingRegistrationMapper.selectFileBorrowingRegistrationList(fileBorrowingRegistration);
    }

    @Override
    public TableDataInfo<FileBorrowingRegistration> selectPageFileBorrowingRegistrationList(FileBorrowingRegistration fileBorrowingRegistration, PageQuery pageQuery) {
        Page<FileBorrowingRegistration> fileBorrowingRegistrationPage = fileBorrowingRegistrationMapper.selectJoinPage(pageQuery.build(), FileBorrowingRegistration.class, this.buildQueryWrapper(fileBorrowingRegistration));
        //Page<FileBorrowingRegistration> page = fileBorrowingRegistrationMapper.selectFileBorrowingRegistrationList(this.buildQueryWrapper(fileBorrowingRegistration));
        return TableDataInfo.build(fileBorrowingRegistrationPage);
    }

    private MPJLambdaWrapper<FileBorrowingRegistration> buildQueryWrapper(FileBorrowingRegistration fileBorrowingRegistration) {
        MPJLambdaWrapper<FileBorrowingRegistration> fileBorrowingRegistrationJoinLambdaWrapper = new MPJLambdaWrapper<FileBorrowingRegistration>();
        fileBorrowingRegistrationJoinLambdaWrapper.selectAll(FileBorrowingRegistration.class)
            .selectAs(FilePerson::getName,FileBorrowingRegistration::getBorrowerFromName)
            .selectAs(SysUser::getNickName,FileBorrowingRegistration::getAttnName)
            .leftJoin(SysUser.class, SysUser::getUserId, FileBorrowingRegistration::getAttn)
            .leftJoin(FilePerson.class, FilePerson::getId, FileBorrowingRegistration::getBorrowerFrom)
            .like(StrUtil.isNotBlank(fileBorrowingRegistration.getBorrower()), FileBorrowingRegistration::getBorrower, fileBorrowingRegistration.getBorrower())
            .like(StrUtil.isNotBlank(fileBorrowingRegistration.getBorrowerFromName()), FileBorrowingRegistration::getBorrowerFromName, fileBorrowingRegistration.getBorrowerFromName())
            .eq(BeanUtil.isNotEmpty(fileBorrowingRegistration.getStatus()), FileBorrowingRegistration::getStatus, fileBorrowingRegistration.getStatus());

        return fileBorrowingRegistrationJoinLambdaWrapper;
    }

    /**
     * 新增借阅登记
     *
     * @param fileBorrowingRegistration 借阅登记
     * @return 结果
     */
    @Override
    public int insertFileBorrowingRegistration(FileBorrowingRegistration fileBorrowingRegistration) {
        fileBorrowingRegistration.setCreateTime(DateExtUtils.getNowDate());
        return fileBorrowingRegistrationMapper.insertFileBorrowingRegistration(fileBorrowingRegistration);
    }

    /**
     * 修改借阅登记
     *
     * @param fileBorrowingRegistration 借阅登记
     * @return 结果
     */
    @Override
    public int updateFileBorrowingRegistration(FileBorrowingRegistration fileBorrowingRegistration) {
        fileBorrowingRegistration.setUpdateTime(DateExtUtils.getNowDate());
        return fileBorrowingRegistrationMapper.updateFileBorrowingRegistration(fileBorrowingRegistration);
    }

    /**
     * 批量删除借阅登记
     *
     * @param ids 需要删除的借阅登记ID
     * @return 结果
     */
    @Override
    public int deleteFileBorrowingRegistrationByIds(Long[] ids) {
        return fileBorrowingRegistrationMapper.deleteFileBorrowingRegistrationByIds(ids);
    }

    /**
     * 删除借阅登记信息
     *
     * @param id 借阅登记ID
     * @return 结果
     */
    @Override
    public int deleteFileBorrowingRegistrationById(Long id) {
        return fileBorrowingRegistrationMapper.deleteFileBorrowingRegistrationById(id);
    }
}
